package com.lei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lei.common.CustomException;
import com.lei.entity.Category;
import com.lei.entity.Dish;
import com.lei.entity.Setmeal;
import com.lei.mapper.CategoryMapper;
import com.lei.service.CategoryService;
import com.lei.service.DishService;
import com.lei.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lei
 * @verson:1.8
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper();
        //查询当前分类是否关联菜品，如果已经关联，抛出异常
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishLambdaQueryWrapper);
        if (count > 0) {
            ////关联菜品，抛出异常
            throw new CustomException("当前分类关联菜品,不能删除");
        }
        //查询当前分类是否关联套餐，如果已经关联，抛出异常
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        if (count1 > 0) {
            //关联套餐，抛出异常
            throw new CustomException("当前分类关联套餐,不能删除");
        }
        super.removeById(id);

    }
}
